package com.jxwifi.cloud.quickclean.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.captcha.Captcha;


/**
 * @author oushuhua
 */
public class CaptchaDialog {

    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private String captchaCode;
    private String cutImage;
    private String srcImage;
    private int xposition;
    private int yposition;
    private ImageView close_captcha;
    private  CaptchaDialogListner captchaDialogListner;
    private Dialog mDialog;
    public Captcha captCha;
    private float density;

    public CaptchaDialog(Context context, String captchaCode, String cutImage, String srcImage, int xposition, int yposition,float density, CaptchaDialogListner captchaDialogListner) {
        if (mDialog == null) {
            mDialog = new Dialog(context, com.idroid.R.style.loading_dialog);
            this.captchaDialogListner = captchaDialogListner;
            View root = View.inflate(context, R.layout.pop_captcha, null);
            close_captcha = (ImageView) root.findViewById(R.id.close_captcha);
            captCha = (Captcha) root.findViewById(R.id.captCha);
            captCha.setBitmap(stringToBitmap(srcImage));
            //captCha.setBitmap(R.mipmap.cat);
            captCha.cDensty = density;
            captCha.setVertifyBlockitmap(stringToBitmap(cutImage));
            captCha.setBlockInfo(xposition,yposition);

            Log.i(TAG,"captCha.cDensty = "+captCha.cDensty);
            //captCha.setBlockInfo(Math.round(xposition*density),Math.round(yposition*density));
            captCha.setCaptchaListener(new Captcha.CaptchaListener() {
                @Override
                public String onAccess(long time) {
                    Toast.makeText(mContext, "验证成功", Toast.LENGTH_SHORT).show();
                    return "验证通过";
                }

                @Override
                public String onFailed(int count) {
                    Toast.makeText(mContext, "验证失败,失败次数" + count, Toast.LENGTH_SHORT).show();
                    return "验证失败";
                }

                @Override
                public String onMaxFailed() {
                    Toast.makeText(mContext, "验证超过次数，你的帐号被封锁", Toast.LENGTH_SHORT).show();
                    return "可以走了";
                }

                @Override
                public void getXp(int x) {
                    setX(x);
                   // captchaDialogListner.getX(x);
                }

                @Override
                public void refresh() {
                    refresh();
                }

            });
            mDialog.setContentView(root);
        }
        close_captcha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void refresh(){
        captchaDialogListner.refresh();
    }
    public void setX(int x){
        captchaDialogListner.getX(x);
    }
    public CaptchaDialog setContentText(String txt) {

        return this;
    }

    public void reset(){
        captCha.reset(false);
    }

    public CaptchaDialog setTitleText(String txt) {

        return this;
    }

    public void setCancelable(boolean cancelable) {
        //mDialog.setCancelable(cancelable);
        mDialog.setCanceledOnTouchOutside(cancelable);
    }



    public void show() {
        if (mDialog != null) {
            mDialog.show();
            mDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        }
    }

    public void dismiss() {
        if (mDialog != null)
            mDialog.dismiss();
    }

    /**
     * 解码Base64图片
     * @param string
     * @return
     */
    public Bitmap stringToBitmap(String string) {
        Bitmap bitmap = null;
        try {
            //byte[] bitmapArray = Base64.decode(string.split(",")[1], Base64.DEFAULT);
            byte[] bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    public interface CaptchaDialogListner {
        void getX(int X);
        void refresh();
    }



}
